package com.wawayaya.bookreader.worddict;

import android.text.TextUtils;

import com.wawayaya.bookreader.bean.StringDouble;
import com.wawayaya.util.TLog;
import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean;
import com.wawayaya.bookreader.Imutualaction;
import com.wawayaya.util.BooleanHolder;

public class TrdGetWordDictdescSound extends Thread {

	private final static int HTTPCONNECTION_CONNECTIONTIMEOUT = 10000;
	private final static int HTTPCONNECTION_READTIMEOUT = 6000;
	
	private BooleanHolder m_bcontinue = new BooleanHolder(true); 
	private AtomicBoolean m_bisruning = new AtomicBoolean(false);
	private int m_ibookid = -1;
	private int m_inetid = -1;
	private int m_iviewid = -1;
	private String m_sbaseword = null;
	private StringDouble[] m_sdSounds = null;
	private Imutualaction m_callback = null;

	public TrdGetWordDictdescSound(int ibookid, int inetid, int iviewid, String sbaseword, StringDouble[] sdSounds, Imutualaction callback) {
		m_ibookid = ibookid;
		m_inetid = inetid;
		m_iviewid = iviewid;
		m_sbaseword = sbaseword;
		m_sdSounds = sdSounds;
		m_callback = callback;
	}

	public void topause() {
		TLog.log("Sound topause " + m_sbaseword);
        m_bcontinue.set(false);
	}
	
	public void pausefinal() {
		m_bcontinue.set(false);
		TLog.log("Sound pausefinal " + m_sbaseword);
		if(Thread.currentThread().equals(this)) {
			// the thread calling "pausefinal" is not the thread TrdGetWordDictdescImage
			return;
		}
		int idx = 0;
		while(m_bisruning.get() && idx++ < 10){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
			}
		}
	}
		
	@Override
	public void run() {
		if(null == m_sdSounds || m_sdSounds.length <= 0) {
			return;
		}

        TLog.log("Sound start " + m_sbaseword);
		m_bisruning.set(true);
		try{
			for(int i = 0; i < m_sdSounds.length; i++){
				if(TextUtils.isEmpty(m_sdSounds[i].m_sVal1) || TextUtils.isEmpty(m_sdSounds[i].m_sVal2)){
					continue;
				}
				try{
					try{
                        File soundfile = new File(m_sdSounds[i].m_sVal2);
                        if (soundfile.exists()) {
                            soundfile.delete();
                        }
					}catch(Exception e){
						e.printStackTrace();
					}
					if(Simpledownloadonesmallfile.downloadonefile(m_sdSounds[i].m_sVal1, m_sdSounds[i].m_sVal2, m_bcontinue,
							HTTPCONNECTION_CONNECTIONTIMEOUT, HTTPCONNECTION_READTIMEOUT)) {
						TLog.log("Sound downloadonefile return true");
                        if(!m_bcontinue.get()){
                            try{
                                new File(m_sdSounds[i].m_sVal2).delete();
                            }catch(Exception e){
                            }
                            return;
                        }
				    } else {
						TLog.log("Sound downloadonefile return false");
                        try{
                            new File(m_sdSounds[i].m_sVal2).delete();
                        }catch(Exception e){
							e.printStackTrace();
                        }
                        return;
                    }
                    if(!m_bcontinue.get()){
                        return;
                    }
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if(!m_bcontinue.get()){
				return;
			}
			if(null != m_callback) {
                boolean sound_ok = true;
				for(int i = 0; i < m_sdSounds.length; i++){
                    File soundfile = new File(m_sdSounds[i].m_sVal2);
					if(soundfile.exists() && soundfile.length() > 0){
						//m_callback.postworddictsound(m_ibookid, m_inetid, m_iviewid, m_sbaseword, m_sdSounds);
						//break;
					} else {
                        sound_ok = false;
                    }
				}
                if (sound_ok) {
                    m_callback.postworddictsound(m_ibookid, m_inetid, m_iviewid, m_sbaseword, m_sdSounds);
                }
			}		
		}finally{
			m_bisruning.set(false);
		}
	}
	
}
